<?php
/**
 * 服务订单支付异步处理控制器
 */
namespace app\api\controller\v1;
use think\Db;
use app\service\OrderService;
use app\service\PaymentService;
class Ordernotify extends Base
{
	public function _initialize()
	{
		parent::_initialize();
	}

	/**
	 * 处理微信支付
	 */
	public function weixin()
	{
		//支付方式
		$payment_id = 1;
		$payment_array = PaymentService::$payment_array;
		$payment = $payment_array[$payment_id];
		if(empty($payment))
        {
        	exit();
        }
        $pay_name = 'payment\\'.$payment['payment'];
        $pay_ret = (new $pay_name($payment['config']['user']))->Respond(array_merge(input('get.'), input('post.')));
        if(!isset($pay_ret['code']) || $pay_ret['code'] != 0)
        {
        	exit();
        }
        //查询订单信息
        $order_info = Db::name('order')->where(['order_no' => $pay_ret['data']['out_trade_no']])->find();
        // 支付处理
        $pay_params = [
                'order'     => $order_info,
                'payment'   => $payment,
                'pay'       => [
                'trade_no'      => $pay_ret['data']['trade_no'],
                'subject'       => $pay_ret['data']['subject'],
                'buyer_user'    => $pay_ret['data']['buyer_user'],
                'pay_price'     => $pay_ret['data']['pay_price'],
            ],
        ];
        // 支付结果处理
        $ret = OrderService::OrderPayHandle($pay_params);
        if($ret['code'] == 0)
        {
        	echo "success";
        }
	}

    /**
     * 支付宝支付异步处理
     */
    public function alipay()
    {
        //支付方式
        $payment_id = 2;
        $payment_array = PaymentService::$payment_array;
        $payment = $payment_array[$payment_id];
        if(empty($payment))
        {
            exit();
        }
        $pay_name = 'payment\\'.$payment['payment'];
        $pay_ret = (new $pay_name($payment['config']['user']))->Respond(array_merge(input('get.'), input('post.')));
        if(!isset($pay_ret['code']) || $pay_ret['code'] != 0)
        {
            exit();
        }
        //查询订单信息
        $order_info = Db::name('order')->where(['order_no' => $pay_ret['data']['out_trade_no']])->find();
        // 支付处理
        $pay_params = [
            'order'     => $order_info,
            'payment'   => $payment,
            'pay'       => [
                'trade_no'      => $pay_ret['data']['trade_no'],
                'subject'       => $pay_ret['data']['subject'],
                'buyer_user'    => $pay_ret['data']['buyer_user'],
                'pay_price'     => $pay_ret['data']['pay_price'],
            ],
        ];
        // 支付结果处理
        $ret = OrderService::OrderPayHandle($pay_params);
        if($ret['code'] == 0)
        {
            echo "success";
        }
    }
    
    
    
    
    /**
	 * 处理微信支付----加钟
	 */
	public function jzweixin()
	{
		//支付方式
		$payment_id = 1;
		$payment_array = PaymentService::$payment_array;
		$payment = $payment_array[$payment_id];
		if(empty($payment))
        {
        	exit();
        }
        $pay_name = 'payment\Weixin';
        $pay_ret = (new $pay_name($payment['config']['user']))->Respond(array_merge(input('get.'), input('post.')));
        if(!isset($pay_ret['code']) || $pay_ret['code'] != 0)
        {
        	exit();
        }
        //查询订单信息
        $order_info = Db::name('order_jz')->where(['order_no' => $pay_ret['data']['out_trade_no']])->find();
        // 支付处理
        $pay_params = [
                'order'     => $order_info,
                'payment'   => $payment,
                'pay'       => [
                'trade_no'      => $pay_ret['data']['trade_no'],
                'subject'       => $pay_ret['data']['subject'],
                'buyer_user'    => $pay_ret['data']['buyer_user'],
                'pay_price'     => $pay_ret['data']['pay_price'],
            ],
        ];
        // 支付结果处理
        $ret = OrderService::OrderPayHandlejz($pay_params);
        if($ret['code'] == 0)
        {
        	echo "success";
        }
	}

    /**
     * 支付宝支付异步处理----加钟
     */
    public function jzalipay()
    {
        //支付方式
        $payment_id = 2;
        $payment_array = PaymentService::$payment_array;
        $payment = $payment_array[$payment_id];
        if(empty($payment))
        {
            exit();
        }
        $pay_name = 'payment\\'.$payment['jzpayment'];
        $pay_ret = (new $pay_name($payment['config']['user']))->Respond(array_merge(input('get.'), input('post.')));
        if(!isset($pay_ret['code']) || $pay_ret['code'] != 0)
        {
            exit();
        }
        //查询订单信息
        $order_info = Db::name('order_jz')->where(['order_no' => $pay_ret['data']['out_trade_no']])->find();
        // 支付处理
        $pay_params = [
            'order'     => $order_info,
            'payment'   => $payment,
            'pay'       => [
                'trade_no'      => $pay_ret['data']['trade_no'],
                'subject'       => $pay_ret['data']['subject'],
                'buyer_user'    => $pay_ret['data']['buyer_user'],
                'pay_price'     => $pay_ret['data']['pay_price'],
            ],
        ];
        // 支付结果处理
        $ret = OrderService::OrderPayHandlejz($pay_params);
        if($ret['code'] == 0)
        {
            echo "success";
        }
    }
    
    
    
    /**
	 * 处理微信支付----升级
	 */
	public function upweixin()
	{
		//支付方式
		$payment_id = 1;
		$payment_array = PaymentService::$payment_array;
		$payment = $payment_array[$payment_id];
		if(empty($payment))
        {
        	exit();
        }
        $pay_name = 'payment\Weixin';
        $pay_ret = (new $pay_name($payment['config']['user']))->Respond(array_merge(input('get.'), input('post.')));
        if(!isset($pay_ret['code']) || $pay_ret['code'] != 0)
        {
        	exit();
        }
        //查询订单信息
        $order_info = Db::name('order_upgrade')->where(['order_no' => $pay_ret['data']['out_trade_no']])->find();
        // 支付处理
        $pay_params = [
                'order'     => $order_info,
                'payment'   => $payment,
                'pay'       => [
                'trade_no'      => $pay_ret['data']['trade_no'],
                'subject'       => $pay_ret['data']['subject'],
                'buyer_user'    => $pay_ret['data']['buyer_user'],
                'pay_price'     => $pay_ret['data']['pay_price'],
            ],
        ];
        // 支付结果处理
        $ret = OrderService::OrderPayHandleup($pay_params);
        if($ret['code'] == 0)
        {
        	echo "success";
        }
	}
	
	//充值
	public function rechange(){
	    //支付方式
		$payment_id = 1;
		$payment_array = PaymentService::$payment_array;
		$payment = $payment_array[$payment_id];
		if(empty($payment))
        {
        	exit();
        }
        $pay_name = 'payment\Weixin';
        $pay_ret = (new $pay_name($payment['config']['user']))->Respond(array_merge(input('get.'), input('post.')));
        if(!isset($pay_ret['code']) || $pay_ret['code'] != 0)
        {
        	exit();
        }
        //查询订单信息
        $order_info = Db::name('user_rechange')->where(['order_no' => $pay_ret['data']['out_trade_no']])->find();
        // 支付处理
        $pay_params = [
                'order'     => $order_info,
                'payment'   => $payment,
                'pay'       => [
                'trade_no'      => $pay_ret['data']['trade_no'],
                'subject'       => $pay_ret['data']['subject'],
                'buyer_user'    => $pay_ret['data']['buyer_user'],
                'pay_price'     => $pay_ret['data']['pay_price'],
            ],
        ];
        // 支付结果处理
        $ret = OrderService::OrderPayHandle_rechange($pay_params);
        if($ret['code'] == 0)
        {
        	echo "success";
        }
	}
}